package com.uwo.house.service.serviceImpl;

import com.uwo.house.beans.request.DesignOrderStatusReq;
import com.uwo.house.beans.request.MeasureCenterReq;
import com.uwo.house.beans.request.MeasureCenterSurveyReq;
import com.uwo.house.beans.response.MeasureOrderResp;
import com.uwo.house.dao.DesignOrderStatusDao;
import com.uwo.house.dao.MeasureCenterDao;
import com.uwo.house.dao.MeasureCenterSurveyDao;
import com.uwo.house.dao.OrderMeasureDao;
import com.uwo.house.service.IMeasureCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by PHS on 2017/7/19.
 */

@Service
public class MeasureCenterService implements IMeasureCenterService {
    @Autowired
    private MeasureCenterDao measureCenterDao;
    @Autowired
    private MeasureCenterSurveyDao measureCenterSurveyDao;
    @Autowired
    private OrderMeasureDao orderMeasureDao;
    @Autowired
    private DesignOrderStatusDao designOrderStatusDao;

    public List<MeasureOrderResp> getMeasureOrderTo(String orderId, Date orderDateFrom, Date orderDateTo, int orderSts) {
        return orderMeasureDao.getMeasureOrderTo(orderId,orderDateFrom,orderDateTo,orderSts);
    }

    public int addMeasureInfo(MeasureCenterReq measureCenterReq) {
        return measureCenterDao.addMeasureInfo(measureCenterReq);
    }

    public int addSurvey(MeasureCenterSurveyReq measureCenterSurveyReq) {
        return measureCenterSurveyDao.addSurvey(measureCenterSurveyReq);
    }

    public List<MeasureCenterReq> rewardList(String userId) {
        return measureCenterDao.rewardList(userId);
    }

    public int reward(MeasureCenterReq measureCenterReq){
        return measureCenterDao.reward(measureCenterReq);
    }

    @Transactional
    public int addMeasureInfoAndSurvey(MeasureCenterReq measureCenterReq, MeasureCenterSurveyReq measureCenterSurveyReq) {
        // 存入测量信息上传表
        int result1 = addMeasureInfo(measureCenterReq);
        // 存入测量问卷调查表
        int result2 = addSurvey(measureCenterSurveyReq);
        // 获取合同编号
        String orderId = measureCenterReq.getContractNumber();
        // 更新客户订单表状态
        int result3 = orderMeasureDao.updateStatus(orderId);
        //初始化设计订单分配信息
        DesignOrderStatusReq designOrderStatusReq = new DesignOrderStatusReq();
        designOrderStatusReq.setOrderId(measureCenterSurveyReq.getOrderId());//订单id
        int result4 = designOrderStatusDao.addOrderStatus(designOrderStatusReq);
        if (result2 == 1 && result1 == 1 && result3 == 1 && result4 == 1) {
            return 1;
        }
        return 0;
    }

    public int receiveOrder(String orderId, String userId) {
        return orderMeasureDao.receiveOrder(orderId,userId);
    }

    public List<MeasureCenterReq> getMeasureOrderToDesign(String orderId, Date assignDateFrom, Date assignDateTo, Integer orderStatus, String userId) {
        return measureCenterDao.getMeasureOrderToDesign(orderId, assignDateFrom, assignDateTo, orderStatus, userId);
    }

    public List<MeasureCenterReq> getMeasureOrderToProjectManager(String orderId, String mobile, Integer projectStatus) {
        return measureCenterDao.getMeasureOrderToProjectManager(orderId,mobile,projectStatus);
    }
}
